{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: Complex Hierarchical Aspect Extraction with Sub-Aspects and Concepts"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: Complex Hierarchical Aspect Extraction with Sub-Aspects and Concepts\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import (\n",
        "    Aspect,\n",
        "    BooleanConcept,\n",
        "    Document,\n",
        "    DocumentLLM,\n",
        "    NumericalConcept,\n",
        "    StringConcept,\n",
        ")\n",
        "\n",
        "\n",
        "# Create a document instance\n",
        "doc = Document(\n",
        "    raw_text=(\n",
        "        \"Software Development and Licensing Agreement\\n\"\n",
        "        \"\\n\"\n",
        "        \"1. Intellectual Property Rights\\n\"\n",
        "        \"1.1 Ownership of Developed Software\\n\"\n",
        "        \"All software developed under this Agreement shall remain the exclusive property of the Developer. \"\n",
        "        \"The Client receives a non-exclusive license to use the software as specified in Section 2.\\n\"\n",
        "        \"\\n\"\n",
        "        \"1.2 Client Data and Content\\n\"\n",
        "        \"The Client retains all rights to data and content provided to the Developer. \"\n",
        "        \"The Developer may not use Client data for any purpose other than fulfilling this Agreement.\\n\"\n",
        "        \"\\n\"\n",
        "        \"1.3 Third-Party Components\\n\"\n",
        "        \"The software may include third-party open-source components. The Client agrees to comply \"\n",
        "        \"with all applicable open-source licenses.\\n\"\n",
        "        \"\\n\"\n",
        "        \"2. License Terms\\n\"\n",
        "        \"2.1 Grant of License\\n\"\n",
        "        \"Developer grants Client a perpetual, non-transferable license to use the software \"\n",
        "        \"for internal business purposes only, limited to 100 concurrent users.\\n\"\n",
        "        \"\\n\"\n",
        "        \"2.2 License Restrictions\\n\"\n",
        "        \"Client may not redistribute, sublicense, or create derivative works. \"\n",
        "        \"Reverse engineering is prohibited except as required by law.\\n\"\n",
        "        \"\\n\"\n",
        "        \"3. Payment and Financial Terms\\n\"\n",
        "        \"3.1 Development Fees\\n\"\n",
        "        \"Total development fee is $150,000, payable in three installments: \"\n",
        "        \"$50,000 upon signing, $50,000 at 50% completion, and $50,000 upon delivery.\\n\"\n",
        "        \"\\n\"\n",
        "        \"3.2 Ongoing License Fees\\n\"\n",
        "        \"Annual license fee of $12,000 is due each year starting from the first anniversary. \"\n",
        "        \"Fees may increase by up to 5% annually with 60 days notice.\\n\"\n",
        "        \"\\n\"\n",
        "        \"3.3 Payment Terms\\n\"\n",
        "        \"All payments due within 30 days of invoice. Late payments incur 1.5% monthly penalty.\\n\"\n",
        "        \"\\n\"\n",
        "        \"4. Liability and Risk Allocation\\n\"\n",
        "        \"4.1 Limitation of Liability\\n\"\n",
        "        \"Developer's total liability shall not exceed the total amount paid under this Agreement. \"\n",
        "        \"Neither party shall be liable for indirect, consequential, or punitive damages.\\n\"\n",
        "        \"\\n\"\n",
        "        \"4.2 Indemnification\\n\"\n",
        "        \"Client agrees to indemnify Developer against third-party claims arising from Client's use \"\n",
        "        \"of the software, except for claims related to Developer's IP infringement.\\n\"\n",
        "        \"\\n\"\n",
        "        \"4.3 Insurance Requirements\\n\"\n",
        "        \"Developer shall maintain professional liability insurance of at least $1,000,000. \"\n",
        "        \"Client shall maintain general liability insurance of at least $2,000,000.\\n\"\n",
        "    ),\n",
        ")\n",
        "\n",
        "# Define a complex hierarchical structure\n",
        "contract_aspects = [\n",
        "    Aspect(\n",
        "        name=\"Intellectual Property Provisions\",\n",
        "        description=\"All provisions related to intellectual property rights, ownership, and usage\",\n",
        "        aspects=[\n",
        "            Aspect(\n",
        "                name=\"Software Ownership\",\n",
        "                description=\"Clauses defining who owns the developed software and related IP rights\",\n",
        "                concepts=[\n",
        "                    StringConcept(\n",
        "                        name=\"Software Owner\",\n",
        "                        description=\"The party that owns the developed software\",\n",
        "                    ),\n",
        "                    BooleanConcept(\n",
        "                        name=\"Exclusive Ownership\",\n",
        "                        description=\"Whether the ownership is exclusive to one party\",\n",
        "                    ),\n",
        "                ],\n",
        "            ),\n",
        "            Aspect(\n",
        "                name=\"Client Data Rights\",\n",
        "                description=\"Provisions about client data ownership and developer's permitted use\",\n",
        "                concepts=[\n",
        "                    StringConcept(\n",
        "                        name=\"Data Usage Restrictions\",\n",
        "                        description=\"Limitations on how developer can use client data\",\n",
        "                    ),\n",
        "                ],\n",
        "            ),\n",
        "            Aspect(\n",
        "                name=\"Third-Party Components\",\n",
        "                description=\"Terms regarding use of third-party or open-source components\",\n",
        "                concepts=[\n",
        "                    BooleanConcept(\n",
        "                        name=\"Open Source Included\",\n",
        "                        description=\"Whether the software includes open-source components\",\n",
        "                    ),\n",
        "                ],\n",
        "            ),\n",
        "        ],\n",
        "    ),\n",
        "    Aspect(\n",
        "        name=\"License Grant and Restrictions\",\n",
        "        description=\"Terms defining the software license granted to the client and any restrictions\",\n",
        "        aspects=[\n",
        "            Aspect(\n",
        "                name=\"License Scope\",\n",
        "                description=\"The extent and limitations of the license granted\",\n",
        "                concepts=[\n",
        "                    StringConcept(\n",
        "                        name=\"License Type\",\n",
        "                        description=\"The type of license granted (exclusive, non-exclusive, etc.)\",\n",
        "                    ),\n",
        "                    NumericalConcept(\n",
        "                        name=\"User Limit\",\n",
        "                        description=\"Maximum number of concurrent users allowed\",\n",
        "                        numeric_type=\"int\",\n",
        "                    ),\n",
        "                    BooleanConcept(\n",
        "                        name=\"Perpetual License\",\n",
        "                        description=\"Whether the license is perpetual or time-limited\",\n",
        "                    ),\n",
        "                ],\n",
        "            ),\n",
        "            Aspect(\n",
        "                name=\"Usage Restrictions\",\n",
        "                description=\"Prohibited uses and activities under the license\",\n",
        "                concepts=[\n",
        "                    BooleanConcept(\n",
        "                        name=\"Redistribution Allowed\",\n",
        "                        description=\"Whether client can redistribute the software\",\n",
        "                    ),\n",
        "                    BooleanConcept(\n",
        "                        name=\"Derivative Works Allowed\",\n",
        "                        description=\"Whether client can create derivative works\",\n",
        "                    ),\n",
        "                ],\n",
        "            ),\n",
        "        ],\n",
        "    ),\n",
        "    Aspect(\n",
        "        name=\"Financial Terms\",\n",
        "        description=\"All payment-related provisions including fees, schedules, and penalties\",\n",
        "        concepts=[\n",
        "            NumericalConcept(\n",
        "                name=\"Total Development Fee\",\n",
        "                description=\"The total amount for software development\",\n",
        "                numeric_type=\"float\",\n",
        "            ),\n",
        "            NumericalConcept(\n",
        "                name=\"Annual License Fee\",\n",
        "                description=\"Yearly fee for using the software\",\n",
        "                numeric_type=\"float\",\n",
        "            ),\n",
        "            NumericalConcept(\n",
        "                name=\"Payment Due Days\",\n",
        "                description=\"Number of days to make payment after invoice\",\n",
        "                numeric_type=\"int\",\n",
        "            ),\n",
        "        ],\n",
        "    ),\n",
        "    Aspect(\n",
        "        name=\"Risk and Liability Management\",\n",
        "        description=\"Provisions for managing risks, liability limitations, and insurance requirements\",\n",
        "        aspects=[\n",
        "            Aspect(\n",
        "                name=\"Liability Limitations\",\n",
        "                description=\"Caps and exclusions on each party's liability\",\n",
        "                concepts=[\n",
        "                    StringConcept(\n",
        "                        name=\"Liability Cap\",\n",
        "                        description=\"Maximum amount of liability for each party\",\n",
        "                    ),\n",
        "                    StringConcept(\n",
        "                        name=\"Excluded Damages\",\n",
        "                        description=\"Types of damages that are excluded from liability\",\n",
        "                    ),\n",
        "                ],\n",
        "            ),\n",
        "            Aspect(\n",
        "                name=\"Insurance Requirements\",\n",
        "                description=\"Required insurance coverage for each party\",\n",
        "                concepts=[\n",
        "                    NumericalConcept(\n",
        "                        name=\"Developer Insurance Amount\",\n",
        "                        description=\"Minimum professional liability insurance for developer\",\n",
        "                        numeric_type=\"float\",\n",
        "                    ),\n",
        "                    NumericalConcept(\n",
        "                        name=\"Client Insurance Amount\",\n",
        "                        description=\"Minimum general liability insurance for client\",\n",
        "                        numeric_type=\"float\",\n",
        "                    ),\n",
        "                ],\n",
        "            ),\n",
        "        ],\n",
        "    ),\n",
        "]\n",
        "\n",
        "# Add all aspects to the document\n",
        "doc.add_aspects(contract_aspects)\n",
        "\n",
        "# Configure DocumentLLM with your API parameters\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/gpt-4.1\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# Extract aspects and concepts\n",
        "doc = llm.extract_all(doc)\n",
        "\n",
        "# Access the hierarchical extraction results\n",
        "print(\"=== CONTRACT ANALYSIS RESULTS ===\\n\")\n",
        "\n",
        "for main_aspect in doc.aspects:\n",
        "    print(f\"{main_aspect.name.upper()}\")\n",
        "    for item in main_aspect.extracted_items:\n",
        "        print(f\"- {item.value}\")\n",
        "\n",
        "    # Access main aspect concepts\n",
        "    if main_aspect.concepts:\n",
        "        print(\"  Main Aspect Concepts:\")\n",
        "        for concept in main_aspect.concepts:\n",
        "            print(f\"    \u2022 {concept.name}:\")\n",
        "            for item in concept.extracted_items:\n",
        "                print(f\"      - {item.value}\")\n",
        "\n",
        "    # Access sub-aspects\n",
        "    if main_aspect.aspects:\n",
        "        print(\"  Sub-Aspects:\")\n",
        "        for sub_aspect in main_aspect.aspects:\n",
        "            print(f\"    {sub_aspect.name}\")\n",
        "            for item in sub_aspect.extracted_items:\n",
        "                print(f\"    - {item.value}\")\n",
        "\n",
        "            # Access sub-aspect concepts\n",
        "            if sub_aspect.concepts:\n",
        "                print(\"    Sub-Aspect Concepts:\")\n",
        "                for concept in sub_aspect.concepts:\n",
        "                    print(f\"      \u2022 {concept.name}:\")\n",
        "                    for item in concept.extracted_items:\n",
        "                        print(f\"        - {item.value}\")\n",
        "\n",
        "    print()\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}